package cc.yiueil;

import cc.yiueil.params.UserRequestParam;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/main")
public class Controller {

    // http://localhost:8080/03_springmvc_params_accept_war_exploded/main/base?name=zhangsan&age=16
    @GetMapping(value = "/base")
    public String base(@RequestParam String name, @RequestParam Integer age) {
        return "Hello name = " + name + ", age = " + age;
    }


    // http://localhost:8080/03_springmvc_params_accept_war_exploded/main/pojo?username=zhangsan&age=16
    @GetMapping(value = "/pojo")
    public String pojo(UserRequestParam user) {
        return user.toString();
    }

    // http://localhost:8080/03_springmvc_params_accept_war_exploded/main/array?ids=1&ids=2&ids=4
    @GetMapping(value = "/array")
    public String array(Integer[] ids) {
        return Arrays.toString(ids);
    }

    @PostMapping(value = "/list")
    public String list(@RequestBody List<UserRequestParam> userRequestParamList) {
        return userRequestParamList.toString();
    }

    // http://localhost:8080/03_springmvc_params_accept_war_exploded/main/date?date=2024-12-22 18:10:00&localDateTime=2024-12-22 18:10:00
    @GetMapping(value = "/date")
    public String date(@RequestParam Date date, @RequestParam LocalDateTime localDateTime) {
        System.out.println("date = " + date);
        System.out.println("localDateTime = " + localDateTime);
        return date.toString() + " " + localDateTime.toString();
    }

    // http://localhost:8080/03_springmvc_params_accept_war_exploded/main/map?name=zs&name=ls&ids=2&ids=3
    @GetMapping(value = "/map")
    public String map(@RequestParam MultiValueMap<String, String> multiValueMap) {
        return multiValueMap.toString();
    }

    @GetMapping(value = "/cookie")
    public String cookie(@CookieValue("username") String username) {
        return "cookie.username = " + username;
    }

    @GetMapping(value = "/header")
    public String header(@RequestHeader("Content-Language") String contentLanguage) {
        return "header.Content-Language = " + contentLanguage;
    }
}
